﻿//
//  Monkey Manager
//  Copyright (C) 2012 Victor Tereschenko
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//     
// ========================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ManagerDB.FolderDBFilters;

namespace ManagerDB
{
    /// <summary>
    /// Treats folder as database. Stores information in XML files.
    /// Can't add/delete files.
    /// </summary>
    public class FolderDB : IDatabase
    {
        public FolderDB(string folder)
        {
            mFolder = folder;
            mFiles = FolderScanner.Scan(folder);
            mHashes = new FileHashes(folder);
        }

        string mFolder;
        List<StoredFile> mFiles;
        FileHashes mHashes;

        public StoredFile Find(string tth, long size)
        {
            return mFiles.Find(file => file.File.ID.TTH == tth);
        }

        public StoredFile[] Find(Filter filter)
        {
            IFilter itemFilter = buildFilter(filter);
            if (itemFilter == null)
                return mFiles.ToArray();

            List<StoredFile> items = new List<StoredFile>();
            foreach (StoredFile item in mFiles.FindAll(item => itemFilter.IsVisible(item)))
            {
                items.Add(item);
            }

            return items.ToArray();
        }

        public void Update(StoredFile file)
        {
            foreach (FileDescription fileDesc in file.Files)
            {
                var path = mHashes.GetPath(fileDesc.ID.TTH, fileDesc.ID.Size);
                if (path != null)
                    path.WatchedCount = file.File.Count;
            }
        }

        public string GetPath(StoredFile file)
        {
            foreach (FileDescription fileDesc in file.Files)
            {
                var path = mHashes.GetPath(fileDesc.ID.TTH, fileDesc.ID.Size);
                if (path != null)
                {
                    file.File.Count = path.WatchedCount;
                    return path.Path;
                }
            }
            return null;
        }

        bool mDisposed = false;
        public void Dispose()
        {
            if (mDisposed)
                return;
            mDisposed = true;
            mHashes.Dispose();
        }

        IFilter buildFilter(Filter filter)
        {
            IFilter itemFilter = null;
            if (filter.WatchStatus == WatchStatus.Watched)
                itemFilter = new WatchedFilter(true, itemFilter);
            else if (filter.WatchStatus != WatchStatus.All)
                itemFilter = new WatchedFilter(false, itemFilter);

            return itemFilter;
        }
    }
}
